package cn.itsource.org.controller;

import cn.itsource.basic.other.annotation.Repeat;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.org.domain.User;
import cn.itsource.org.service.IUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @GetMapping("/checkphone/{phone}")
    public AjaxResult checkphone(@PathVariable String phone) {
        boolean result = userService.checkPhone(phone);
        return AjaxResult.toAjax(result);
    }

    @Repeat
    @GetMapping("/sendmobilecode/{phone}")
    @ApiOperation(value = "发送验证码")
    public AjaxResult sendMobileCode(@PathVariable String phone, HttpServletRequest request) {
        //验证手机号
        boolean result = userService.sendMobileCode(phone, request);
        return AjaxResult.toAjax(result, "发送验证码成功!");
    }

    @PostMapping("/register")
    @ApiOperation(value = "注册账号")
    public AjaxResult registerUser(@RequestBody @Validated(User.PhoneRegister.class) User user) {
        boolean result = userService.registerUser(user);
        return AjaxResult.toAjax(result);
    }
}
